import 'package:flutter/material.dart';

/*
Future在未来只会获取一个值。Stream的字面意思是水流，
Stream不像Future那样只会在未来获取一个值，它可以异步获取0个或者多个值。如果说Future是一个
异步版本的int或者String，Stream则更像是异步版本的列表，List,List,列表里面可能会有0个或者多个
元素。
 */
class StramDemo extends StatefulWidget {
  const StramDemo({super.key});

  @override
  State<StramDemo> createState() => _StramDemoState();
}

class _StramDemoState extends State<StramDemo> {
  final future = Future.delayed(const Duration(seconds: 1), () => 43);
  final stream = Stream.periodic(const Duration(seconds: 1), (value) => value);

  @override
  void initState() {
    super.initState();

    future.then((value) => print(value));
    stream.listen((event) {
      print("stream:$event");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Future 测试Demo'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          ElevatedButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(Colors.blue),
                foregroundColor: MaterialStateProperty.all(Colors.white),
                elevation: MaterialStateProperty.all(20),
                shape: MaterialStateProperty.all(RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10))),
              ),
              onPressed: () {
                print('点击测试..');
              },
              child: const Text('Stream 测试')),
          SizedBox(height: 50),
        ],
      ),
    );
  }
}
